<?php
/*
Plugin Name: Floating NextPrev
Plugin URI: http://www.ferramentasblog.com/2011/09/power-comment-validacao-de-comentarios.html
Description: Exiba &iacute;cones para navega&ccedil;&atilde;o entre seus posts de forma pratica e r&aacute;pida.
Author: Claudio Sanches
Version: 1.2
Author URI: http://www.claudiosmweb.com/
*/

// Criar menu para o plugin no WP
function add_fnextprev_menu() {
    add_options_page('Floating NextPrev', 'Floating NextPrev', 'manage_options', 'floating-nextprev', 'admin_fnextprev');
}
add_action('admin_menu', 'add_fnextprev_menu');
// Adicionar opcoes no DB
function set_fnextprev_options() {
    add_option('fnextprev_style','default');
}
// Deleta opcoes quando o plugin &eacute; desinstalado
function unset_fnextprev_options() {
    delete_option('fnextprev_style');
}
// instrucoes ao instalar ou desistalar o plugin
register_activation_hook(__FILE__,'set_fnextprev_options');
register_deactivation_hook(__FILE__,'unset_fnextprev_options');

// Pagina de opcoes
function admin_fnextprev() {
    ?>
    <div class="wrap">
        <div class="icon32" id="icon-options-general"><br /></div>
        <h2>Floating NextPrev Op&ccedil;&otilde;es</h2>
        <?php 
        if(!empty($_POST) && check_admin_referer('fnextprev_nonce_action', 'fnextprev_nonce_field')) {
            update_fnextprev_options();
        }
        print_fnextprev_form();
        ?>
    </div>
    <?php
}
// Validar op&ccedil;&otilde;es
function update_fnextprev_options() {
    $correto = false;
    // Layout do plugin
    if ($_REQUEST['fnextprev_style']) {
        update_option('fnextprev_style', $_REQUEST['fnextprev_style']);
        $correto = true;
    }
    if ($correto) {
        ?><div id="message" class="updated fade">
        <p><?php _e('Op&ccedil;&otilde;es salvas.'); ?></p>
        </div> <?php
    }
    else {
        ?><div id="message" class="error fade">
        <p><?php _e('Erro ao salvar op&ccedil;&otilde;es!'); ?></p>
        </div><?php
    }
}

// Formulario com as opcoes
function print_fnextprev_form() {
    $fnextprev_style = get_option('fnextprev_style');
    $fnextprev_plugin_dir = get_bloginfo('wpurl') . '/wp-content/plugins/floating-nextprev/';
    ?>
    <form action="" method="post">
    <h3 style="margin: 20px 0 -5px;"><?php _e('Apar&ecirc;ncia'); ?></h3>
    <table class="form-table">
        <tr>
            <th scope="row"><label for="fnextprev_style_default"><?php _e('Layout do plugin'); ?></label></th>
            <td>
                <label><input style="margin:0 0 -15px;padding:0;display:block" type="radio" id="fnextprev_style_default" name="fnextprev_style" value="default" <?php if ($fnextprev_style == "default") { _e('checked="checked"'); } ?> /><br /><img src="<?php echo $fnextprev_plugin_dir; ?>opt/opt-default.jpg" alt="default" style="display:block;margin:-17px 0 20px 30px;" /></label>
                <label><input style="margin:0 0 -15px;padding:0;display:block" type="radio" id="fnextprev_show_likefb" name="fnextprev_style" value="likefb" <?php if ($fnextprev_style == "likefb") { _e('checked="checked"'); } ?>/><br /><img src="<?php echo $fnextprev_plugin_dir; ?>opt/opt-likefb.jpg" alt="likefb" style="display:block;margin:-17px 0 20px 30px;" /></label>
                <label><input style="margin:0 0 -15px;padding:0;display:block" type="radio" id="fnextprev_show_likefbred" name="fnextprev_style" value="likefbred" <?php if ($fnextprev_style == "likefbred") { _e('checked="checked"'); } ?>/><br /><img src="<?php echo $fnextprev_plugin_dir; ?>opt/opt-likefbred.jpg" alt="likefbred" style="display:block;margin:-17px 0 20px 30px;" /></label>
                <label><input style="margin:0 0 -15px;padding:0;display:block" type="radio" id="fnextprev_show_likefbgreen" name="fnextprev_style" value="likefbgreen" <?php if ($fnextprev_style == "likefbgreen") { _e('checked="checked"'); } ?>/><br /><img src="<?php echo $fnextprev_plugin_dir; ?>opt/opt-likefbgreen.jpg" alt="likefbgreen" style="display:block;margin:-17px 0 20px 30px;" /></label>
                <label><input style="margin:0 0 -15px;padding:0;display:block" type="radio" id="fnextprev_show_likefbgray" name="fnextprev_style" value="likefbgray" <?php if ($fnextprev_style == "likefbgray") { _e('checked="checked"'); } ?>/><br /><img src="<?php echo $fnextprev_plugin_dir; ?>opt/opt-likefbgray.jpg" alt="likefbgray" style="display:block;margin:-17px 0 20px 30px;" /></label>
                <label><input style="margin:0 0 -15px;padding:0;display:block" type="radio" id="fnextprev_show_likefbgray" name="fnextprev_style" value="btnblue" <?php if ($fnextprev_style == "btnblue") { _e('checked="checked"'); } ?>/><br /><img src="<?php echo $fnextprev_plugin_dir; ?>opt/opt-btnblue.jpg" alt="btnblue" style="display:block;margin:-17px 0 20px 30px;" /></label>
                <label><input style="margin:0 0 -15px;padding:0;display:block" type="radio" id="fnextprev_show_likefbgray" name="fnextprev_style" value="btnred" <?php if ($fnextprev_style == "btnred") { _e('checked="checked"'); } ?>/><br /><img src="<?php echo $fnextprev_plugin_dir; ?>opt/opt-btnred.jpg" alt="btnred" style="display:block;margin:-17px 0 20px 30px;" /></label>
                <label><input style="margin:0 0 -15px;padding:0;display:block" type="radio" id="fnextprev_show_likefbgray" name="fnextprev_style" value="btngreen" <?php if ($fnextprev_style == "btngreen") { _e('checked="checked"'); } ?>/><br /><img src="<?php echo $fnextprev_plugin_dir; ?>opt/opt-btngreen.jpg" alt="btngreen" style="display:block;margin:-17px 0 20px 30px;" /></label>
                <label><input style="margin:0 0 -15px;padding:0;display:block" type="radio" id="fnextprev_show_likefbgray" name="fnextprev_style" value="btngray" <?php if ($fnextprev_style == "btngray") { _e('checked="checked"'); } ?>/><br /><img src="<?php echo $fnextprev_plugin_dir; ?>opt/opt-btngray.jpg" alt="btngray" style="display:block;margin:-17px 0 20px 30px;" /></label>
            </td>
            </td>
        </tr>
    </table>
    <p class="submit">
        <?php wp_nonce_field('fnextprev_nonce_action', 'fnextprev_nonce_field'); ?>
        <input type="submit" class="button-primary" name="submit" value="salvar" />
    </p>
    <p>
        <a style="margin-right:20px;" href="http://www.fbloghost.com/plano-wp-host/" target="_blank" title="FBlogHost - Hospedagem profissional para Worpdress">
            <img style="border:none;" src="<?php echo $fnextprev_plugin_dir; ?>fbloghost.jpg" alt="FBlogHost - Hospedagem profissional para Worpdress" />
        </a>
        <a href="http://www.ferramentasblog.com/ebook-blogar" target="_blank" title="Ebook Bloggar - O Processo de cria&ccedil;&atilde;o de blogs">
            <img style="border:none;" src="<?php echo $fnextprev_plugin_dir; ?>bloggar.jpg" alt="Ebook Bloggar - O Processo de cria&ccedil;&atilde;o de blogs" />
        </a>
    </p>
    </form>
<?php
}
// Chama jquery no Tema
function my_scripts_method() {
    wp_enqueue_script('jquery');
}    
add_action('wp_enqueue_scripts', 'my_scripts_method');
// JS e CSS do plugin no head
function fnextprev_css_head() {
    $fnp_dir = WP_PLUGIN_URL.'/'.str_replace(basename( __FILE__),"",plugin_basename(__FILE__));
    $fnextprev_style = get_option('fnextprev_style');
    $fnextprev_js_default = "<script type=\"text/javascript\" src=\"".$fnp_dir."js/default.js\"></script>\n";
    $fnextprev_js_likefb = "<script type=\"text/javascript\" src=\"".$fnp_dir."js/likefb.js\"></script>\n";
    
    switch($fnextprev_style){
        case 'default':
            $fnextprev_css = "<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"".$fnp_dir."css/default.css\" />\n";
            $fnextprev_js = $fnextprev_js_default;
            break;
        case 'likefb':
            $fnextprev_css = "<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"".$fnp_dir."css/likefb.css\" />\n";
            $fnextprev_js = $fnextprev_js_likefb;
            break;
        case 'likefbred':
            $fnextprev_css = "<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"".$fnp_dir."css/likefbred.css\" />\n";
            $fnextprev_js = $fnextprev_js_likefb;
            break;
        case 'likefbgreen':
            $fnextprev_css = "<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"".$fnp_dir."css/likefbgreen.css\" />\n";
            $fnextprev_js = $fnextprev_js_likefb;
            break;
        case 'likefbgray':
            $fnextprev_css = "<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"".$fnp_dir."css/likefbgray.css\" />\n";
            $fnextprev_js = $fnextprev_js_likefb;
            break;
        case 'btnblue':
            $fnextprev_css = "<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"".$fnp_dir."css/btnblue.css\" />\n";
            $fnextprev_js = $fnextprev_js_default;
            break;
        case 'btnred':
            $fnextprev_css = "<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"".$fnp_dir."css/btnred.css\" />\n";
            $fnextprev_js = $fnextprev_js_default;
            break;
        case 'btngreen':
            $fnextprev_css = "<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"".$fnp_dir."css/btngreen.css\" />\n";
            $fnextprev_js = $fnextprev_js_default;
            break;
        case 'btngray':
            $fnextprev_css = "<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"".$fnp_dir."css/btngray.css\" />\n";
            $fnextprev_js = $fnextprev_js_default;
            break;
    }
    
    if(is_single()) {
        echo $fnextprev_css . $fnextprev_js;
    }
}
add_filter('wp_head', 'fnextprev_css_head');
// Adiciona Floating NextPrev no Footer
function fnextprev_add_box($content) {
    if(is_single()) {
        ?>
<div id="fnp-nav">
    <div class="fnp-previous"><?php previous_post_link('%link', '<div class="fnp-box-left"><div class="fnp-content-left"><div class="fnp-content-border"></div><div class="fnp-nav-title">Anterior</div><div class="fnp-nav-link">%title</div></div></div>') ?></div>
    <div class="fnp-next"><?php next_post_link('%link', '<div class="fnp-box-right"><div class="fnp-content-right"><div class="fnp-content-border"></div><div class="fnp-nav-title">Pr&oacute;ximo</div><div class="fnp-nav-link">%title</div></div></div>') ?></div>
</div>
<?php
    }
}
add_filter('wp_footer', 'fnextprev_add_box');
?>